package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.dto.CheckInApplyDto;
import com.zzyl.nursing.dto.CheckInElderDto;
import com.zzyl.nursing.mapper.*;
import com.zzyl.nursing.vo.CheckInConfigVo;
import com.zzyl.nursing.vo.CheckInElderVo;
import com.zzyl.nursing.vo.ElderFamilyVo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanExpressionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.service.ICheckInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 入住Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-06
 */
@Service
public class CheckInServiceImpl extends ServiceImpl<CheckInMapper, CheckIn> implements ICheckInService {
    @Autowired
    private CheckInMapper checkInMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;


    /**
     * 查询入住
     *
     * @param //id 入住主键
     * @return 入住
     */
    @Override
    public void selectCheckInById(Long id   ) {
        //判断老人是否存在
        CheckIn checkIn = checkInMapper.selectCheckInById(id);

        if ( checkIn == null) {
            throw new BeanExpressionException("老人不存在");
        }
        //查询老人信息
        Elder elder = elderMapper.selectElderById(checkIn.getElderId());
        CheckInElderVo checkInElderVo = new CheckInElderVo();
        BeanUtils.copyProperties(elder, checkInElderVo);

        //查询入住配置信息
        CheckInConfigVo checkInConfigVo = new CheckInConfigVo();
         BeanUtils.copyProperties(checkIn, checkInConfigVo);
        System.out.println(checkInConfigVo);

        //查询合同信息
        LambdaQueryWrapper<Contract> QueryWrapper = Wrappers.lambdaQuery();
        QueryWrapper.eq(Contract::getElderId, checkIn.getElderId());
        Contract contract = contractMapper.selectOne(QueryWrapper);


   //查询老人家属的信息
       elder.setRemark(elder.getRemark());
       BeanUtils.copyProperties(elder, checkInElderVo);

    }

    @Override
    public void findById(Long id) {

    }


    /**
     * 查询入住列表
     *
     * @param checkIn 入住
     * @return 入住
     */
    @Override
    public List<CheckIn> selectCheckInList(CheckIn checkIn) {
        return checkInMapper.selectCheckInList(checkIn);
    }

    @Override
    public int insertCheckIn(CheckIn checkIn) {
        return 0;
    }



    /**
     * 新增入住
     *
     * @param checkIn 入住
     * @return 结果
     */


    /**
     * 修改入住
     *
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int updateCheckIn(CheckIn checkIn) {
        return updateById(checkIn) ? 1 : 0;
    }

    /**
     * 批量删除入住
     *
     * @param ids 需要删除的入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除入住信息
     *
     * @param id 入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public void apply(CheckInApplyDto dto) {

        LambdaQueryWrapper<CheckIn> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CheckIn::getIdCardNo, dto.getCheckInElderDto().getIdCardNo())
                .eq(CheckIn::getStatus, 1);
        CheckIn checkIn = checkInMapper.selectOne(queryWrapper);


        // 2. 判断老人是否已经入住
        if (checkIn != null) {
            throw new BeanExpressionException("老人已经入住");
        }
        //3.判断床位是否占用
        Bed bed = getBed(dto);

        //4.增加老人信息
        Elder elder = getElder(dto, bed);
        //查询老人的基本信息
        Elder elder1 = getElder(dto);
        //添加创建时间
        elder.setCreateTime(new Date());
        if (ObjectUtils.isNotEmpty(elder1)) {
            elderMapper.updateElder(elder1);
        } else {
            elderMapper.insertElder(elder);
        }
        //5.增加入住信息
        CheckIn checkIn1 = getCheckIn(dto, elder, bed);

        //5.新增合同信息
        extracted(dto, elder);

        //6.增加入住配置信息
        extracted(dto, checkIn1);


    }

    private void extracted(CheckInApplyDto dto, CheckIn checkIn1) {
        CheckInConfig checkInConfig = new CheckInConfig();
        BeanUtils.copyProperties(dto.getCheckInConfigDto(), checkInConfig);
        checkInConfig.setCheckInId(checkIn1.getId());
        //判断是否为空
        CheckInConfig checkInConfig1 = checkInConfigMapper.selectCheckInConfigById(checkInConfig.getId());
        if (ObjectUtils.isNotEmpty(checkInConfig1)) {
            checkInConfigMapper.updateCheckInConfig(checkInConfig);
        }

        checkInConfigMapper.insert(checkInConfig);
    }

    private static void extracted(CheckInApplyDto dto, Elder elder) {
        Contract contract = new Contract();
        BeanUtils.copyProperties(dto.getCheckInContractDto(), contract);
        contract.setElderId(elder.getId());
        contract.setElderName(elder.getName());
        contract.setStartDate(dto.getCheckInConfigDto().getStartDate());
        contract.setEndDate(dto.getCheckInConfigDto().getEndDate());
        contract.setStatus(1);
    }

   @NotNull
    private CheckIn getCheckIn(CheckInApplyDto dto, Elder elder, Bed bed) {
        CheckIn checkIn1 = new CheckIn();
        BeanUtils.copyProperties(dto.getCheckInConfigDto(), checkIn1);

        checkIn1.setElderId(elder.getId());
        checkIn1.setElderName(elder.getName());
        checkIn1.setBedNumber(bed.getBedNumber());
        checkIn1.setIdCardNo(elder.getIdCardNo());
        checkIn1.setNursingLevelName(dto.getCheckInConfigDto().getNursingLevelName());
        checkIn1.setStartDate(dto.getCheckInConfigDto().getStartDate());
        checkIn1.setEndDate(dto.getCheckInConfigDto().getEndDate());
        checkIn1.setSortOrder(1);
        checkIn1.setStatus(0);
        // 判断 是否为空
        CheckIn checkIn2 = checkInMapper.selectCheckInById(checkIn1.getId());
        if (ObjectUtils.isNotEmpty(checkIn2)) {
            checkInMapper.updateCheckIn(checkIn1);
        } else {
            checkInMapper.insertCheckIn(checkIn1);
        }
        return checkIn1;
    }

    private Elder getElder(CheckInApplyDto dto) {
        LambdaQueryWrapper<Elder> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.eq(Elder::getId, dto.getCheckInElderDto().getIdCardNo())
                .eq(Elder::getId, dto.getCheckInElderDto().getIdCardNo())
                .ne(Elder::getStatus, 1);
        Elder elder1 = elderMapper.selectOne(queryWrapper1);
        return elder1;
    }

    @NotNull
    private static Elder getElder(CheckInApplyDto dto, Bed bed) {
        Elder elder = new Elder();

        BeanUtils.copyProperties(dto.getCheckInElderDto(), elder);
        elder.setBedId(bed.getId());
        elder.setBedNumber(bed.getBedNumber());

        elder.setStatus(1);
        return elder;
    }

    @NotNull
    private Bed getBed(CheckInApplyDto dto) {
        Bed bed = bedMapper.selectBedById(dto.getCheckInConfigDto().getBedId());//获取床位信息
        if (bed.getBedStatus() == 0) {
            bed.setBedStatus(1);
            bedMapper.updateBed(bed);
        }
        return bed;
    }

}
